package com.zhuge_new.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.pinball83.maskededittext.MaskedEditText;
import com.maning.mndialoglibrary.MToast;
import com.zhuge_new.Base.BaseActivity;
import com.zhuge_new.R;
import com.zhuge_new.untils.ActivityStack;

import org.w3c.dom.Text;

/**
 * Created by lenovo on 2018-08-30.
 */

public class CreditAddition2Activity extends BaseActivity implements View.OnClickListener {


    private String bank_number;
    private EditText et_priod;
    private EditText et_cvn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_creditadd2ition);
        init_title();
    }

    public void init_title() {
        super.init_title();
        Intent intent = getIntent();
        bank_number = intent.getStringExtra("bank_number");
        String bank = intent.getStringExtra("bankname");
        TextView tv_card = findViewById(R.id.tv_card);
        String splitStr = bank_number.replaceAll("\\d{4}(?!$)", "$0 ");
        tv_card.setText(splitStr);
        TextView bankname = findViewById(R.id.bankname);
        bankname.setText(bank);
        final TextView tv_proid = findViewById(R.id.tv_proid);
        et_cvn = findViewById(R.id.et_cvn);
        TextView tv_title = findViewById(R.id.tv_nav_title);
        tv_title.setText("资金效验");
        ImageView iv_bank = findViewById(R.id.iv_nav_back);
        iv_bank.setOnClickListener(this);
        final TextView tv_cvn = findViewById(R.id.tv_cvn);
        Button next_step = findViewById(R.id.next_step);
        et_priod = findViewById(R.id.et_priod);
        final LinearLayout card1 = findViewById(R.id.cred1);
        final LinearLayout card2 = findViewById(R.id.cred2);
        et_priod.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String etn = s.toString();
                String splitStr = etn.replaceAll("\\d{2}(?!$)", "$0/");
                tv_proid.setText(splitStr);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        next_step.setOnClickListener(this);
        et_cvn.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String etn = s.toString();
                tv_cvn.setText(etn);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        et_cvn.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    //获取到交焦点
                    Log.e("abc获取到交焦点", String.valueOf(hasFocus));
                    card1.setVisibility(View.GONE);
                    card2.setVisibility(View.VISIBLE);
                } else {
                    //失去焦点
                    Log.e("abc失去焦点", String.valueOf(hasFocus));
                    card1.setVisibility(View.VISIBLE);
                    card2.setVisibility(View.GONE);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_nav_back:
                ActivityStack.getScreenManager().popOneActivity(CreditAddition2Activity.class);
                break;
            case R.id.next_step:
                if (et_priod.length() < 0) {
                    MToast.makeTextShort(CreditAddition2Activity.this, "请输入卡片的有效期").show();
                } else if (et_cvn.length() < 0) {
                    MToast.makeTextShort(CreditAddition2Activity.this, "请输入卡片的验证码").show();
                } else {
                    Intent intent = new Intent(CreditAddition2Activity.this, CreditAddition3Activity.class);
                    intent.putExtra("ptiod", et_priod.getText().toString());
                    intent.putExtra("cvn", et_cvn.getText().toString());
                    intent.putExtra("bank_number", bank_number);
                    startActivity(intent);
                    break;
                }
        }
    }
}
